<?php
class zbModel extends appModel
{
	public function getZbhbActiveList($page)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where .= " 1=1  order by  id desc ";
		$result=$this->get_page(" * ",$this->table('zbhb_info'),$where,$page,'20');
		$listPage['pagelink']=$this->getpagelink();
			
		foreach ($result as $k => $v)
		{
			if($v['onoff']==0)
			{
				$result[$k]['onoffShow'] = "关闭";
			}
			else
			{
				$result[$k]['onoffShow'] = "开启";
			}
			if($v['status']==1)
			{
				$result[$k]['statusShow'] = "已启用";
			}
			else
			{
				$result[$k]['statusShow'] = "准备中";
			}
			$sql = "SELECT count(*) FROM ".$this->table("zbhb_hongbao")." WHERE lid = ".$v['id']." AND sp_ticket!='' ";
			$result[$k]["total"] = $this->getOne($sql);
		}
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('zbhb_info')." WHERE ".$where;
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	
	public function getZbhbActive($id)
	{
		$sql = "SELECT * FROM ".$this->table('zbhb_info')." WHERE id = $id";
		$result =  $this->getRow($sql);
		$sql = "SELECT count(*) FROM ".$this->table("zbhb_hongbao")." WHERE lid = ".$result['id']." AND sp_ticket!='' ";
		$result["total"] = $this->getOne($sql);
		return $result;
	}
	
	public function get_hongbao_list($page,$id)
	{
		$listPage=array('data'=>'','pageLink'=>'');
			
		$where .= " 1=1 and lid=$id order by id desc ";
		$result=$this->get_page(" * ",$this->table('zbhb_hongbao'),$where,$page,'20');
		$listPage['pagelink']=$this->getpagelink();
			
		foreach ($result as $k => $v)
		{
			if($v['hb_type']==1)
			{
				$result[$k]['hb_typeShow'] = "裂变红包";
			}
			else
			{
				$result[$k]['hb_typeShow'] = "普通红包";
			}
			
			if(!$v['sp_ticket'])
			{
				$result[$k]['hb_typeShow'] = "<span style='color:red;'>无效红包</span>";
			}
		}
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('zbhb_hongbao')." WHERE ".$where;
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	
	
	public function get_mch_billno($mch_id,$app='',$time)
	{
		$mch_billno = hy_substr($mch_id.date('Ymd',$time).$app, 28);
		$sql = "SELECT COUNT(id) FROM ".$this->table("zbhb_hongbao")." WHERE mch_billno = '".$mch_billno."'";
		$count = $this->getOne($sql);
		if($count)
		{//有值
			$append = rand_match(10);
			$this->get_out_trade_no($mch_id,$append,$time);
		}
		else
		{//无值
			return $mch_billno;
		}
	}
	
	public function del_repeat($lid)
	{
		$sql =" select id FROM ".$this->table('zbhb_hongbao')." WHERE id IN ( "
				." SELECT id FROM ".$this->table('zbhb_hongbao')." WHERE lid=$lid GROUP BY detail_id HAVING count(1) > 1 )";
		$result = $this->getAll($sql);
		foreach ($result as $k=>$v)
		{
			$sql = "DELETE FROM ".$this->table('zbhb_hongbao')." WHERE id = ".$v['id'];
			$this->query($sql);
		}
	}
	
	public function getZbhbNotPull($id)
	{
		$sql = "SELECT * FROM ".$this->table('zbhb_info')." WHERE id = $id";
		$result =  $this->getRow($sql);
		$sql = "SELECT count(*) FROM ".$this->table("zbhb_hongbao")." WHERE lid = ".$result['id']." AND status=1";
		$result["nototal"] = $this->getOne($sql);
		$sql = "SELECT count(*) FROM ".$this->table("zbhb_hongbao")." WHERE lid = ".$result['id'];
		$result["total"] = $this->getOne($sql);
		return $result;
	}
	
	public function getZbhbListTicketNoPush($id)
	{
		$sql ="SELECT * FROM ".$this->table("zbhb_hongbao")." WHERE lid = $id AND sp_ticket!='' AND status=1 group by sp_ticket LIMIT 100 ";
		$result = $this->getAll($sql);
		$res = array();
		$res2 = array();
		$i=0;
		foreach ($result as $k=>$v)
		{
			$res[$i] = array("ticket"=>$v['sp_ticket']);
			$res2[$i] = array("id"=>$v['id']);
			$i++;
		}
		$resu['list'] = $res;
		$resu['idlist'] = $res2;
		return $resu;
	}
	
	public function updateHongbaoStatus($ids,$status){
		for($i=0;$i<count($ids);$i++)
		{
			$sql = "UPDATE ".$this->table('zbhb_hongbao')." SET status=$status WHERE id=".$ids[$i]['id'];
			$this->query($sql);
		}
	}
	
	public function getHongbaoUserList($page,$id){
		$listPage=array('data'=>'','pageLink'=>'');
		$where .= " 1=1 and p1.hongbao_id=$id order by addtime desc ";
		$result=$this->get_page(" p1.*,p2.nickname,p2.openid ",$this->table('get_hongbao_user')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,'20');
		$listPage['pagelink']=$this->getpagelink();
			
		foreach ($result as $k => $v){
	
		}
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('get_hongbao_user')." p1 WHERE ".$where;
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	
	public function delZbhbRepeat($lid){
		$sql =" select id FROM ".$this->table('zbhb_hongbao')." WHERE id IN ( "
				." SELECT id FROM ".$this->table('zbhb_hongbao')." WHERE lid=$lid GROUP BY detail_id HAVING count(1) > 1 )";
		$result = $this->getAll($sql);
		foreach ($result as $k=>$v){
			$sql = "DELETE FROM ".$this->table('zbhb_hongbao')." WHERE id = ".$v['id'];
			$this->query($sql);
		}
	}
}